import Vue from 'vue'

//按需导入mint-UI中的组件
// import{ Header,Swipe,SwipeItem,Button,Lazyload} from 'mint-ui'  
// Vue.component(Header.name,Header)
// Vue.component(Swipe.name,Swipe)
// Vue.component(SwipeItem.name,SwipeItem)
// Vue.component(Button.name,Button)
// Vue.use(Lazyload)
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.use(MintUI)

import Vuex from 'vuex'
Vue.use(Vuex)

var car = JSON.parse(localStorage.getItem('car') || '[]')

var store = new Vuex.Store({
    state: {
        car: car
    },
    mutations: {
        addToCar(state,goodsinfo){
            //点击加入购物车，商品保存到car
            var flag = false;
            //购物车中没有找到相应的函数
            state.car.some(item =>{
                if(item.id == goodsinfo.id){
                    item.count += parseInt(goodsinfo.count)
                    flag = true
                    return true
                }
            })
            if(!flag){
                state.car.push(goodsinfo)
            }
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        updateCar(state,goodsinfo){
            //修改购物车中 商品数量
            state.car.some(item=>{
                if(item.id == goodsinfo.id){
                    item.count = parseInt(goodsinfo.count);
                    return true
                }
            })
            //所有结果循环完毕之后，要把东西放入本地存储中
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        removeFromCar(state,id){
            state.car.some((item,i)=>{
                if(item.id == id){
                    state.car.splice(i,1)
                    return true
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        updateStatus(state,info){
            state.car.some(item => {
                if(item.id == info.id){
                    item.selected = info.selected
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        }
    },
    getters:{
        //相当于计算属性，也相当于过滤器
        getAllCount(state){
            var c  = 0;
            state.car.forEach(item => {
                c += item.count
            })
            return c
        },
        getgoodsCount(state){
            var o ={};
            state.car.forEach(item =>{
                o[item.id] = item.count
            })
            return o;
        },
        getgoodStatus(state){
            var s = {};
            state.car.forEach(item => {
                s[item.id] = item.selected
            })
            return s;
        },
        getcountAndamout(state){
            var o = {
                count: 0,
                amount: 0
            };
            state.car.forEach(item=>{
                if(item.selected){
                    o.count += item.count;
                    o.amount += item.count * item.price
                }
            })
            return o;

        }

    }

})
//导入路由包
import VueRouter from 'vue-router'
//安装路由
Vue.use(VueRouter)
//导入自己的路由模块
import router from './router.js'

//导入vue-resource
import VueResource from 'vue-resource'
//安装vue-resource
Vue.use(VueResource)

//全局设置post时候表单数据格式
Vue.http.options.emulateJSON = true;


import moment from 'moment'
//定义全局的过滤器

Vue.filter('dateFormat',function(dataStr,pattern = "YYYY-MM-DD HH:mm:ss"){
    return moment(dataStr).format(pattern)
})

//导入MUI的样式
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'

//安装图片预览插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview)
import app from './App.vue'
import { isNode } from 'postcss-selector-parser';
 var vm = new Vue({
     el: "#app",
     render: c=>c(app),
     router, //挂载路由对象到VM实例上
     store 
 })